﻿using CRMExam.Database;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace CRMExam.Controllers
{
    public class UserInvitation
    {
        public string Url { get; set; }
        public string Message { get; set; }

    }
    public class InvitationController : ApiController
    {
        public IHttpActionResult Get(string id)
        {
            if (id == "password")
            {
                try
                {
                    Guid guid = Guid.NewGuid();

                    using (CRMExam1Entities context = new CRMExam1Entities())
                    {
                        context.InvitedUsers.Add(new InvitedUser { Id = guid });
                        context.SaveChanges();
                    }

                    UserInvitation result = new UserInvitation
                    {
                        Message = "We welcome you to register as a new user, just paste the link url into a browser",
                        //Url = "http://localhost:65505/InviteUser/Invite/" + guid.ToString()
                        Url = ConfigurationManager.AppSettings["HostServer"] + "/InviteUser/Invite/" + guid.ToString()
                    };

                    return Ok(result);

                }
                catch (Exception)
                {
                    return NotFound();
                }

            }
            else
            {
                return NotFound();
            }
        }
    }
}
